package cn.jsj.invite.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.jsj.invite.dao.EnterpriseDao;
import cn.jsj.invite.dao.InvitationDao;
import cn.jsj.invite.dao.StudentDao;
import cn.jsj.invite.pojo.EnterpriseEnt;
import cn.jsj.invite.pojo.InvitationInfo;
import cn.jsj.invite.pojo.StudentEnt;
import cn.jsj.invite.service.EnterpriseService;
import cn.jsj.invite.service.PictureFileService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class EnterpriseServiceImpl implements EnterpriseService {

    private final EnterpriseDao enterpriseDao;
    private final InvitationDao invitationDao;
    private final StudentDao studentDao;
    private final PictureFileService pictureFileService;

    @Autowired
    public EnterpriseServiceImpl(EnterpriseDao enterpriseDao, InvitationDao invitationDao, StudentDao studentDao, PictureFileService pictureFileService) {
        this.enterpriseDao = enterpriseDao;
        this.invitationDao = invitationDao;
        this.studentDao = studentDao;
        this.pictureFileService = pictureFileService;
    }

    @Override
    public int register(EnterpriseEnt info) {
        int i = 0;
        try {
            i = enterpriseDao.insertOneEnterprise(info);
        } catch (Exception e) {
            //插入数据触发唯一约束，插入失败
            log.error("unique line", e);
        }
        return i;
    }

    @Override
    public int publishEnterpriseInvitation(InvitationInfo info, MultipartFile[] photo) {
        //检查企业是否有此人
        Integer initiatorId = info.getInitiatorId();
        EnterpriseEnt enterpriseEnt = enterpriseDao.selectEnterpriseById(initiatorId.longValue());
        if (enterpriseEnt == null) {
            return 0;
        }
        //设置帖子类型为企业贴
        info.setInvitationType(4);
        //设置正文换行符有效存入数据库
        info.setContext(info.getContext().replace("\n", "\r\n"));
        //设置企业的名称
        info.setEnterpriseName(enterpriseEnt.getEnterpriseName());
        //保存图片，图片名格式化后存到数据库
        int length = photo.length;
        if (length != 0) {
            ArrayList<String> photoNameArray = new ArrayList<>();
            int i = 0;
            for (MultipartFile file : photo) {
                if (i == 3) {
                    break;
                }
                //此处相对路径 = 文件名 + 扩展名，直接存即可
                String relativePath = pictureFileService.savePictureFile(file);
                photoNameArray.add(relativePath);
                i++;
            }
            String join = String.join(",", ArrayUtil.toArray(photoNameArray, String.class));
            info.setPicList(join);
        }
        return enterpriseDao.insertOneEnterpriseInvitation(info);
    }

    @Override
    public List<InvitationInfo> listEnterpriseInvitation(Integer enterpriseId) {
        List<InvitationInfo> infos = invitationDao.selectInvitationByEnterpriseId(enterpriseId);
        InvitationServiceImpl.invitationInfoDataFormat(infos);
        return infos;
    }

    @Override
    public int updatePasswordById(String oldPassword, String newPassword, int id) {
        if (oldPassword != null && newPassword != null && id > 0) {
            return enterpriseDao.updatePasswordById(oldPassword, newPassword, id);
        }
        return 0;
    }

    @Override
    public PageInfo<StudentEnt> listStudentIsOpen(Integer schoolId, Integer pageNumber) {
        pageNumber = pageNumber != null ? pageNumber : 1;
        PageHelper.startPage(pageNumber, 15);
        List<StudentEnt> studentEnts = studentDao.selectStudentIsOpenBySchoolId(schoolId);
        //页码共5个
        return new PageInfo<>(studentEnts, 5);
    }

    @Override
    public Integer removeOnePageById(Integer pageId) {
        if (pageId == null) {
            return null;
        }
        return enterpriseDao.deleteOnePageById(pageId);
    }

    @Override
    public Integer updateEnterpriseInfo(EnterpriseEnt enterpriseEnt) {
        EnterpriseEnt enterprise = enterpriseDao.selectEnterpriseById(enterpriseEnt.getId());
        if (enterprise == null){
            return null;
        }
        return enterpriseDao.updateOneEnterpriseInfo(enterpriseEnt);

    }

}
